import { Component, DoCheck } from '@angular/core';
import { NavController } from 'ionic-angular';
import { DetailsPage } from '../details/details';
import * as $ from 'jquery';
import { DataService } from './services/data.service';

@Component({
  selector: 'page-data',
  templateUrl: 'data.html'
})
export class DataPage implements DoCheck {

  gaming: string = "tension-em";
  oldgaming: string = "tension-em";
  show = false;
  data;
  Alldata;
  Alltype = [];
  search;
  text = "";
  constructor(
    public navCtrl: NavController,
    public service: DataService) { }
  itemSelected(e) {
    this.navCtrl.push(DetailsPage, {
      data: e
  
    });
  }
  // getItems(e) {
  //   this.search = [];
  //   //如果点击叉号就不执行
  //   if (e.cancelable) {
  //     this.text = "";
  //     this.show = false;
  //     return;
  //   }
  //   //如果是删除键
  //   if (e.data == null) {
  //     const arr = this.text.split("");
  //     arr.pop()
  //     this.text = arr.join("");
  //   }
  //   //如果是输入文字
  //   else {
  //     this.text += e.data.toLocaleUpperCase();
  //   }
  //   if (this.text !== "") {
  //     this.show = true;
  //     this.Alltype.map(item => {
  //       if (item.label.indexOf(this.text.toLocaleUpperCase()) !== -1) {
  //         this.search.push(item)
  //       }
  //     })
  //   } else {
  //     this.show = false;
  //   }
  // }

  ionViewDidLoad() {
    this.service.getTree().subscribe(res => {
      if (res.lenght == 0) { return }
      if (res.length == 0) {
        return;
      }
      const data = res.data
      this.Alldata = data;
      this.data = this.Alldata[0].children
    })
    setTimeout(() => {
      this.Alldata.map((item, i) => {
        item.children.map(v => {
          this.Alltype.push(v)
        })
      })
    }, 200)
  }
  //离开页面
  ionViewWillLeave() {
    this.show = false;
    $(".searchbar-input").val("")
  }
  ngDoCheck() {
    if (this.gaming !== this.oldgaming) {
      this.oldgaming = this.gaming;
      if (this.gaming == "tension-em") {
        this.data = this.Alldata[0].children
      } else if (this.gaming == "tension-fb") {
        this.data = this.Alldata[1].children
      } else {
        this.data = this.Alldata[2].children
      }
    }
  }
}
